package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjBasicReportBo;
import com.ruoyi.zhgd.domain.vo.ZjBasicReportVo;
import com.ruoyi.zhgd.domain.ZjBasicReport;
import com.ruoyi.zhgd.mapper.ZjBasicReportMapper;
import com.ruoyi.zhgd.service.IZjBasicReportService;

import java.util.*;

/**
 * 报基础格内容Service业务层处理
 *
 * @author tll
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjBasicReportServiceImpl implements IZjBasicReportService {

    private final ZjBasicReportMapper baseMapper;

    /**
     * 查询报基础格内容
     */
    @Override
    public ZjBasicReportVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询报基础格内容列表
     */
    @Override
    public TableDataInfo<ZjBasicReportVo> queryPageList(ZjBasicReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjBasicReport> lqw = buildQueryWrapper(bo);
        Page<ZjBasicReportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报基础格内容列表
     */
    @Override
    public List<ZjBasicReportVo> queryList(ZjBasicReportBo bo) {
        LambdaQueryWrapper<ZjBasicReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjBasicReport> buildQueryWrapper(ZjBasicReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjBasicReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFieldKey()), ZjBasicReport::getFieldKey, bo.getFieldKey());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldVal()), ZjBasicReport::getFieldVal, bo.getFieldVal());
        lqw.eq(StringUtils.isNotBlank(bo.getReportId()), ZjBasicReport::getReportId, bo.getReportId());
        lqw.eq(bo.getCreateDate() != null, ZjBasicReport::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateUser()), ZjBasicReport::getCreateUser, bo.getCreateUser());
        lqw.eq(bo.getUpdateDate() != null, ZjBasicReport::getUpdateDate, bo.getUpdateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateUser()), ZjBasicReport::getUpdateUser, bo.getUpdateUser());
        return lqw;
    }

    /**
     * 新增报基础格内容
     */
    @Override
    public Boolean insertByBo(ZjBasicReportBo bo) {
        ZjBasicReport add = BeanUtil.toBean(bo, ZjBasicReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报基础格内容
     */
    @Override
    public Boolean updateByBo(ZjBasicReportBo bo) {
        ZjBasicReport update = BeanUtil.toBean(bo, ZjBasicReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjBasicReport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除报基础格内容
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据报表id查询报表基础内容列表
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> listMap(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("reportId"))) {
            throw new ServiceException("报表主键ID不能为空");
        }
        // 获取通用接口数据
        List<Map<String, Object>> list = baseMapper.listMap(params);
        // 返回值规范
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> mapResult = new HashMap();
        for (Map<String, Object> map : list) {
            mapResult.put(map.get("fieldKey").toString(), map.get("fieldVal"));
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        mapList.add(mapResult);
        result.put("data", mapList);
        return result;
    }
}
